import request from '/@/utils/request';
import {ElNotification} from "element-plus";

export function fetchList(query?: Object) {
	return request({
		url: '/admin/customer/page',
		method: 'get',
		params: query,
	});
}
export function fetchHistoryList(query?: Object) {
	return request({
		url: '/admin/customer/pageHistory',
		method: 'get',
		params: query,
	});
}

export function addObj(obj?: Object) {
	return request({
		url: '/admin/customer',
		method: 'post',
		data: obj,
	});
}

export function getObj(id?: string) {
	return request({
		url: '/admin/customer/details/' + id,
		method: 'get',
	});
}

export function getObjDetails(obj?: object) {
	return request({
		url: '/admin/customer/details',
		method: 'get',
		params: obj,
	});
}

export function delObj(ids?: object) {
	return request({
		url: '/admin/customer',
		method: 'delete',
		data: ids,
	});
}

export function putObj(obj?: Object) {
	return request({
		url: '/admin/customer',
		method: 'put',
		data: obj,
	});
}
export function protect(id?: Object) {
	return request({
		url: '/admin/customer/protect',
		method: 'post',
		data: {customerId:id},
	});
}
export function unprotect(id?: Object) {
	return request({
		url: '/admin/customer/unprotect',
		method: 'post',
		data: {customerId:id},
	});
}
export const userList = (params?: Object) => {
	return request({
		url: '/admin/user/page',
		method: 'get',
		params,
	});
};
export function validateCustomName(rule: any, value: any, callback: any, isEdit: boolean) {
	if (isEdit) {
		return callback();
	}

	getObjDetails({ customerName: value,protectCustomer:1 }).then((response) => {
		const result = response.data;
		if (result !== null) {
			ElNotification({
				title: '警告',
				dangerouslyUseHTMLString: true,
				message: '<b>客户名称已被保护</b> <br>该客户已被：' + result.createBy+' 保护'+'<br>'+'保护有效期至：'+result.protectCustomerTime ,
				type: 'warning',
			});
			callback(new Error('客户名称已被保护'));
		} else {
			callback();
		}
	});
}
